import OnlineApi from '../api/OnlineAPI'
import env from '../env/env'

export {onlineModule, onlineActionTypes}

const onlineTypes = {
  SAVE_PARAM_DATA: 'ONLINE_SAVE_PARAM_DATA',
  SET_LOADING: 'ONLINE_SET_LOADING',
  SET_LOADED: 'ONLINE_SET_LOADED',
  SET_ASSOCIATION_PLAN_LIST: 'SET_ASSOCIATION_PLAN_LIST',
  SET_ENTERPRISE_COURSE_LIST: 'SET_ENTERPRISE_COURSE_LIST'
};

const onlineActionTypes = {
  UPDATE_ONLINE_DATA_IF_NEED: 'UPDATE_ONLINE_DATA_IF_NEED',
  UPDATE_ONLINE_DATA: 'UPDATE_ONLINE_DATA',
  FETCH_PARAM_DATA: 'FETCH_PARAM_DATA',
  GET_ASSOCIATION_PlAN: 'GET_ASSOCIATION_PlAN',
  GET_ENTERPRISE_COURSE: 'GET_ENTERPRISE_COURSE',
};

const onlineModule = {
  namespaced: true,
  //保存在全局的模块数据
  state: {
    loaded: false,
    //按文件，paramData对应文件param里面的所有参数
    paramData: undefined,
    isLoading: false,
    associationPlanList: null,
    enterpriseCourseList: null
  },
  //获取数据
  getters: {
    param: (state) => state.paramData || {},
    associationPlanList: (state) => state.associationPlanList || {},
    enterpriseCourseList: (state) => state.enterpriseCourseList || {}
  },
  //同步请求，只有这里能修改数据
  mutations: {
    [onlineTypes.SAVE_PARAM_DATA](state, data) {
      state.paramData = data
    },
    [onlineTypes.SET_LOADING](state, data) {
      state.isLoading = data
    },
    [onlineTypes.SET_LOADED](state, data) {
      state.loaded = data
    },
    [onlineTypes.SET_ASSOCIATION_PLAN_LIST](state, data) {
      state.associationPlanList = data
    },
    [onlineTypes.SET_ENTERPRISE_COURSE_LIST](state, data) {
      state.enterpriseCourseList = data
    },
  },
  //异步请求
  actions: {
    [onlineActionTypes.UPDATE_ONLINE_DATA_IF_NEED]({state, commit, dispatch}, payload) {
      if (state.isLoading) return;
      if (state.loaded) return Promise.resolve();
      return dispatch(onlineActionTypes.UPDATE_ONLINE_DATA)
    },
    [onlineActionTypes.UPDATE_ONLINE_DATA]({state, commit, dispatch}, payload) {
      commit(onlineTypes.SET_LOADING, true);
      let course = dispatch(onlineActionTypes.GET_ENTERPRISE_COURSE);
      let plan = dispatch(onlineActionTypes.GET_ASSOCIATION_PlAN);
      return Promise.all([course, plan].filter(it => !!it)).then((it) => {
        commit(onlineTypes.SET_LOADING, false);
        commit(onlineTypes.SET_LOADED, true)
      })
    },

    [onlineActionTypes.FETCH_PARAM_DATA]({commit}, payload) {
      return OnlineApi.getParam().then(data => {
        commit(onlineTypes.SAVE_PARAM_DATA, data);
        return data
      })
    },
    async [onlineActionTypes.GET_ASSOCIATION_PlAN]({commit, getters, rootGetters}, payload) {
      let env = process.env.NODE_ENV === "development" ? 'dev' : '';
      await OnlineApi.getHome(rootGetters.hsId,env).then((result) => {
        Object.keys(result).map((item) => {
          if (item === 'plan') {
            commit(onlineTypes.SET_ASSOCIATION_PLAN_LIST, result[item])
          }
        })

      })
    },
    [onlineActionTypes.GET_ENTERPRISE_COURSE]({commit, getters, rootGetters}, payload) {
      let env = process.env.NODE_ENV === "development" ? 'dev' : '';
      OnlineApi.getHome(rootGetters.hsId,env).then((result) => {
        Object.keys(result).map((item) => {
          if (item === 'course') {
            commit(onlineTypes.SET_ENTERPRISE_COURSE_LIST, result[item])
          }
        })

      })
    },
  },
};


